package com.example.springstudy.listener;

import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.example.springstudy.base.BaseAnalysisEventListener;
import com.example.springstudy.dao.dto.SkuDto;
import com.example.springstudy.entity.Sku;
import com.example.springstudy.entity.Supplier;
import com.example.springstudy.service.SkuService;
import com.example.springstudy.service.SupplierService;
import com.example.springstudy.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class SkuImportListener extends BaseAnalysisEventListener<SkuDto> {

    // 有效条数
    private int validCount;

    // 无效条数
    private int invalidCount;

    StringBuilder msg = new StringBuilder();

    private final SkuService skuService;
    private final SupplierService supplierService;

    public SkuImportListener(){
        this.skuService = SpringUtil.getBean(SkuService.class);
        this.supplierService = SpringUtil.getBean(SupplierService.class);
    }


    @Override
    public void invoke(SkuDto skuDto, AnalysisContext analysisContext) {
        log.info("解析到一条sku:{}", JSONUtil.toJsonStr(skuDto));
        StringBuilder validationMsg = new StringBuilder();

        String skuName = skuDto.getName();
        String skuCode = skuDto.getCode();
        String superior = skuDto.getSupplierName();

        Sku saveBean = new Sku(skuDto);
        if(StrUtil.isBlank(skuName)){
            validationMsg.append("商品名称为空；");
        }
        if(StrUtil.isBlank(skuCode)){
            validationMsg.append("skuId为空；");
        }
        if(validationMsg.isEmpty()){
            Supplier s = supplierService.getOneByName(superior);
            int superiodId = 0;
            if(s==null){
                Supplier saveSuperior = new Supplier();
                saveSuperior.setCreateTime(DateUtils.defaultDate());
                saveSuperior.setName(superior);
                superiodId = supplierService.add(saveSuperior);
            } else {
                superiodId = s.getId();
            }
            saveBean.setSupplierId(superiodId);
            Sku sku = skuService.getOneByCode(skuCode);
            Sku ok = null;
            if(sku == null){
                saveBean.setStock(Integer.parseInt(skuDto.getCount()));
                saveBean.setCreateTime(DateUtils.defaultDate());
                ok = skuService.addSku(saveBean);
            } else {
                saveBean.setId(sku.getId());
                saveBean.setStock(sku.getStock()+Integer.parseInt(skuDto.getCount()));
                ok = skuService.updateSku(saveBean);
            }
            if(ok==null){
                validCount++;
                msg.append("第").append(validCount + invalidCount).append("行数据保存失败；<br/>");
            }
        } else {
            validCount++;
            msg.append("第").append(validCount + invalidCount).append("行数据校验失败；<br/>");
        }
    }


    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        // 在此处添加导入完成后的其他处理逻辑，例如数据校验、数据存储等
        log.info("所有数据解析完成！");
    }

    @Override
    public String getMsg() {
        return StrUtil.format("导入用户结束：成功{}条，失败{}条；<br/>{}", validCount, invalidCount, msg);
    }
}
